package com.bkitmobile.kesu.appengine.web.client.SupportPopup;

import java.util.HashMap;


import com.bkitmobile.kesu.appengine.web.client.GreetingService;
import com.bkitmobile.kesu.appengine.web.client.GreetingServiceAsync;
import com.bkitmobile.kesu.appengine.web.client.entity.PlaceClient;
import com.bkitmobile.kesu.appengine.web.client.utility.LocationInfoPopupConstant;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.rebind.UiBinderGenerator;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

////
public class LocationInfoPopup extends DialogBox  {
        private HashMap<String, Widget> InputParam = 
                new HashMap<String, Widget> ();
        private final FormPanel form = new FormPanel();
        private double Longtitude;
        private double Latitude;
        private final GreetingServiceAsync greetingService = GWT
                        .create(GreetingService.class);
        
        ////// nhung ham ho tro viec kiem tra
        private PlaceClient getLocationClick()
        {
                //// check ten dia diem
                TextBox name = (TextBox) InputParam.get(LocationInfoPopupConstant.LocationName);
                String name_txt = name.getText();
                if (name_txt.isEmpty())
                {
                        Window.alert("Ban chua dien ten dia diem");
                        return null;
                }
                
                ////// check dia chi
                TextBox address = (TextBox) InputParam.get(LocationInfoPopupConstant.LocationAdd);
                String address_txt = address.getText();
                if (address_txt.isEmpty())
                {
                        Window.alert("Ban chua dien dia chi");
                        return null;
                }
                
                /////thong tin dia chi
                RichTextArea info = (RichTextArea) InputParam.get(LocationInfoPopupConstant.LocationInfo);
                String info_txt = info.getText();
                
                /////Thong tin ve loai dia diem
                ListBox type = (ListBox) InputParam.get(LocationInfoPopupConstant.LocationType);
                String type_txt = type.getItemText(type.getSelectedIndex());
                
                PlaceClient _ret = new PlaceClient(type_txt,this.Longtitude,
                                this.Latitude,name_txt,info_txt,address_txt);
                
                
                
                return _ret;
        }
        
        /// nhung ham ho tro tao giao dien
        /**
        * 
        * @param username
        * @return
        */
        private HorizontalPanel createTextPanel (String title)
        {
                HorizontalPanel create = new HorizontalPanel();
                Label name = new Label();
                name.setText(title);
                name.setPixelSize(150, 20);
                create.add(name);
                
                TextBox input = new TextBox();
                input.setPixelSize(300, 20);
                create.add(input);
                InputParam.put(title, input);
                
                return create;
        }
        
        private VerticalPanel createRichTextBox (String title)
        {
                VerticalPanel create = new VerticalPanel();
                Label name = new Label();
                name.setText(title);
                name.setPixelSize(150, 20);
                create.add(name);
                
                RichTextArea input = new RichTextArea();
                input.setPixelSize(450, 100);
                create.add(input);
                InputParam.put(title, input);
                
                return create;
        }
        
        private HorizontalPanel createComboBox (String title, String[] choice)
        {
                HorizontalPanel create = new HorizontalPanel();
                Label name = new Label();
                name.setText(title);
                name.setPixelSize(150, 20);
                create.add(name);
                
                ListBox input = new ListBox();
                input.setPixelSize(300, 20);
                create.add(input);

                this.insertPlace(input, choice);
                InputParam.put(title, input);
                
                return create;
        }
        
        ////insert nhung dia diem private hoac public
        private void insertPlace (ListBox input, String[] premium)
        {
                for (String i:premium)
                {
                        input.addItem(i);
                }
                
        }
        
        private HorizontalPanel createChoiceOfTypePanel ()
        {
                HorizontalPanel create = new HorizontalPanel();
                Label name = new Label();
                name.setText(LocationInfoPopupConstant.LocationChosenTypeStr);
                name.setPixelSize(150, 20);
                create.add(name);
                
                
                RadioButton input1 = new RadioButton(LocationInfoPopupConstant.LocationChosenTypeStr
                                ,"Co tra phi");
                input1.setPixelSize(150, 20);
                input1.setChecked(true);
                input1.addClickListener(new ClickListener() {
                        @Override
                        public void onClick(Widget sender) {
                                // TODO Auto-generated method stub
                                changeType(true);
                        }
                });
                create.add(input1);
                
                RadioButton input2 = new RadioButton(LocationInfoPopupConstant.LocationChosenTypeStr
                                ,"Khong tra phi");
                input2.setPixelSize(150, 20);
                input2.addClickListener(new ClickListener() {
                        @Override
                        public void onClick(Widget sender) {
                                // TODO Auto-generated method stub
                                changeType(false);
                        }
                });
                create.add(input2);
                
                
                return create;
        }
        
        private void changeType (boolean isPremium)
        {
                ListBox chosePlace = (ListBox) InputParam.get(LocationInfoPopupConstant.LocationType);
                int numOfPlace;
                if (isPremium)
                {
                        numOfPlace = LocationInfoPopupConstant.LocationFreeType.length;
                }
                else
                        numOfPlace = LocationInfoPopupConstant.LocationPremiumType.length;
                for (int i = 0;i<numOfPlace;i++)
                        chosePlace.removeItem(0);
                if (isPremium)
                {
                        String[] premium = LocationInfoPopupConstant.LocationPremiumType;
                        for (String i:premium)
                        {
                                chosePlace.addItem(i);
                        }
                }
                else
                {
                        String[] free = LocationInfoPopupConstant.LocationFreeType;
                        for (String i:free)
                        {
                                chosePlace.addItem(i);
                        }
                }
        }
        
        public LocationInfoPopup()
        {
        }
        
        public LocationInfoPopup(double Lat, double Lon)
        {
                this.Longtitude = Lon;
                this.Latitude = Lat;
                this.setText("Thong tin dia diem");
                // Enable animation.
                setAnimationEnabled(true);

                  
                // Enable glass background.
                setGlassEnabled(true);


                
                String [] premium = LocationInfoPopupConstant.LocationPremiumType;
                String [] free = LocationInfoPopupConstant.LocationFreeType ;
                VerticalPanel panel = new VerticalPanel();
                panel.setSpacing(4);

                
                panel.add(this.createTextPanel(LocationInfoPopupConstant.LocationName));
                panel.add(this.createTextPanel(LocationInfoPopupConstant.LocationAdd));
                panel.add(this.createRichTextBox(LocationInfoPopupConstant.LocationInfo));
                panel.add(this.createChoiceOfTypePanel());
                panel.add(this.createComboBox(LocationInfoPopupConstant.LocationType,premium));
                
                Button add_btn = new Button();
                add_btn.setText("Tao");
                Button cancel_btn = new Button();
                cancel_btn.setText("Thoat");
                
                /////// Xu ly su kien add thanh cong
                cancel_btn.addClickListener(new ClickListener() {
                        
                        @Override
                        public void onClick(Widget sender) {
                                // TODO Auto-generated method stub
                                hide();
                        }
                });
                
                add_btn.addClickListener(new addBtnClick());
                
                
                //////// Add vao panel
                HorizontalPanel button = new HorizontalPanel();
                button.add(add_btn);
                button.add(cancel_btn);
                button.setSpacing(5);
                panel.add(button);
                form.setWidget(panel);
                ////// khong dung add vi glass panel se che mat popup
                this.setWidget(form);
                this.setPopupPosition(425, 146);
        }
        
        
        /////Su kien click nut add_btn
        //// DataStore.createPlace(....)
        class addBtnClick implements ClickListener
        {

                @Override
                public void onClick(Widget sender) {
                        // TODO Auto-generated method stub
                        PlaceClient location_click = getLocationClick();
                        if (location_click!=null)
                        {
                                greetingService.addPlace(location_click, new AsyncCallback<String>() {
                                        
                                        @Override
                                        public void onSuccess(String result) {
                                                // TODO Auto-generated method stub
                                        	if (result.equals("failure"))
                                        	{
                                        		Window.alert("So luong dia diem co tra phi cua ban " +
                                        			"da het. Xin ban hay mua them");
                                        		hide();
                                        		FeePayingPopup pay = new FeePayingPopup();
                                        		pay.show();
                                        	}
                                        	else
                                        		Window.Location.reload();
                                        }
                                        
                                        @Override
                                        public void onFailure(Throwable caught) {
                                                // TODO Auto-generated method stub
                                            Window.alert("Co loi xay ra, xin ban dang nhap lai");  
                                            Window.Location.reload();
                                        }
                                });
                                
                        }
                }
                
        }
}
